package cn.example.demo.modules.house.controller;

import cn.example.demo.common.model.response.HttpDataResponseResult;
import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.model.response.HttpResponseResult;
import cn.example.demo.modules.house.dto.ScenerySpotDto;
import cn.example.demo.modules.house.entity.ScenerySpot;
import cn.example.demo.modules.house.service.IScenerySpotService;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.annotation.AuthEnable;
import cn.example.demo.common.validation.constraint.ParamRegex;
import cn.example.demo.common.validation.groups.Insert;
import cn.example.demo.common.validation.groups.Update;
import cn.example.demo.common.validation.groups.Delete;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import cn.hutool.core.util.StrUtil;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 【旅游景点】控制器
 * </p>
 */
@Validated
@RestController
@RequestMapping("api/ScenerySpot")
@Api(tags = {"【旅游景点】操作接口"})
public class ScenerySpotController {
    @Resource
    private IScenerySpotService scenerySpotService;

    @AuthEnable
    @PostMapping("add")
    @ApiOperation(value = "新增【旅游景点】")
    public HttpResponseResult addScenerySpot(@ApiParam(value = "旅游景点请求参数体") @RequestBody @Validated(Insert.class) ScenerySpotDto dto) {
        // 保存ScenerySpot
        ServiceResult result = scenerySpotService.insertScenerySpot(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("modify")
    @ApiOperation(value = "修改【旅游景点】")
    public HttpResponseResult modifyScenerySpot(@ApiParam(value = "旅游景点请求参数体") @RequestBody @Validated(Update.class) ScenerySpotDto dto) {
        // 保存ScenerySpot
        ServiceResult result = scenerySpotService.updateScenerySpot(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @GetMapping(value = "retrieval")
    @ApiOperation(value = "查询【旅游景点】")
    public HttpResponseResult getScenerySpotList(@ApiParam(value = "旅游景点请求参数体") ScenerySpotDto dto) {
        PageBean result = scenerySpotService.queryScenerySpot(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【旅游景点】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @GetMapping(value = "find/{id}")
    @ApiOperation(value = "根据 ID 查询单个【旅游景点】")
    public HttpResponseResult getOneScenerySpot(@ApiParam(value = "旅游景点ID") @PathVariable @NotBlank(message = "id不能为空！") String id) {
        ScenerySpot result = scenerySpotService.findScenerySpotById(id);

        if (result == null) {
            return HttpDataResponseResult.isNotFound(StrUtil.format("未查询到【旅游景点】记录！ID:{}", id), null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @DeleteMapping("remove")
    @ApiOperation(value = "删除【旅游景点】")
    public HttpResponseResult removeScenerySpot(@ApiParam(value = "旅游景点请求参数体") @RequestBody @Validated(Delete.class) ScenerySpotDto dto) {
        ServiceResult result = scenerySpotService.deleteScenerySpot(dto);

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @GetMapping(value = "dictList")
    @ApiOperation(value = "以字典表格式查询【旅游景点】")
    public HttpResponseResult getScenerySpotAsDict(@ApiParam(value = "旅游景点请求参数体") ScenerySpotDto dto) {
        List result = scenerySpotService.queryScenerySpotAsDict(dto);
        if (ObjectUtil.isEmpty(result)) {
            return HttpDataResponseResult.isNotFound("【旅游景点】列表：没有数据！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }
}
